﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Collections;
using System.Transactions;

#region Epoca
namespace Distrackting.DataLayer
{
    public partial class Epoca
    {
        public void setEpoca(Epoca ep)
        {
            this.cod_epoca = ep.cod_epoca;
            this.epoca = ep.epoca;
        }
    }
}
#endregion


#region dataEpoca
namespace Distrackting.DataLayer
{
    public class dataEpoca
    {
        #region Methods
        public static void createEpoca(Epoca ep)
        {

                try
                {
            dbDataDataContext dbData = new dbDataDataContext();
                    ep.cod_epoca = System.Guid.NewGuid();
                        dbData.Epocas.InsertOnSubmit(ep);
                    dbData.SubmitChanges();
                    

                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        

        public static Epoca getEpoca(Guid guid)
        {
            try
            {
                if (guid == null) return null;
                dbDataDataContext dbData = new dbDataDataContext();

                Epoca epc = (from ep in dbData.Epocas
                                where ep.cod_epoca == guid
                                select ep).FirstOrDefault();
                return epc;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Boolean changeEpoca(Epoca ep)
        {
            try
            {
                if (ep == null)
                    return false;
                else
                {
                    dbDataDataContext dbData = new dbDataDataContext();
                    Epoca res = dbData.Epocas.FirstOrDefault(epc => epc.cod_epoca == ep.cod_epoca);
                    if (res == null) return false;
                    else
                    {
                        res.setEpoca(ep);
                        dbData.SubmitChanges();
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Epoca> getEpocas()
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                return dbData.Epocas;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Epoca> getEpocas(Expression<Func<Epoca, bool>> whereClause)
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                return dbData.Epocas.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Boolean deleteEpoca(Epoca ep)
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                if (dbData.Epocas.Contains(ep))
                {
                    Epoca res = (from epc in dbData.Epocas
                                    where epc.cod_epoca == ep.cod_epoca
                                    select epc).FirstOrDefault();
                    if (res == null)
                        return false;
                    else
                    {
                        dbData.Epocas.DeleteOnSubmit(ep);
                        dbData.SubmitChanges();
                        return true;
                    }
                }
                else return false;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        #endregion
    }
}
#endregion